//
//  ProjectDetailCell.swift
//  GitOSC-Swift
//
//  Created by 李萍 on 16/1/13.
//  Copyright © 2016年 李萍. All rights reserved.
//

import UIKit
import SwiftyJSON
import Kingfisher

class ProjectDetailCell: UITableViewCell {

    @IBOutlet weak var userImageView: UIImageView!
    @IBOutlet weak var projectName: UILabel!
    @IBOutlet weak var timeIntevalLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var starButton: UIButton!
    @IBOutlet weak var watchButton: UIButton!
    @IBOutlet weak var staredCountLabel: UILabel!
    @IBOutlet weak var watchedCountLabel: UILabel!
    @IBOutlet weak var createdTimeLabel: UILabel!
    @IBOutlet weak var branchLabel: UILabel!
    @IBOutlet weak var publicLabel: UILabel!
    @IBOutlet weak var languageLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    internal func setContentForDetailProject(detailProject : GTProject) -> Void {
        self.userImageView.kf_setImageWithURL((NSURL.init(string: "https://git.oschina.net/"+(detailProject.owner?.portrait)!)!), placeholderImage:UIImage.init(named: "portrait_loading"))
        self.projectName.text = "\(JSON(detailProject.name!))"
        
        let lastPushTime : String = "\(JSON(detailProject.lastPushAt!))"
        if !(lastPushTime.isEmpty) {
            timeIntevalLabel.text = "更新于" + lastPushTime.substringToIndex(lastPushTime.startIndex.advancedBy(10))
        } else {
            timeIntevalLabel.text = "更新于" + lastPushTime
        }
        if JSON(detailProject.stared!) {
            starButton.setTitle("Unstar", forState: UIControlState.Normal)
        } else {
             starButton.setTitle("Star", forState: UIControlState.Normal)
        }
        
        if JSON(detailProject.wacthed!) {
            watchButton.setTitle("Unwatch", forState: UIControlState.Normal)
        } else {
            watchButton.setTitle("Watch", forState: UIControlState.Normal)
        }
        

        self.contentLabel.text = "\(JSON(detailProject.projectDescription!))"
        staredCountLabel.attributedText = self.setImageForLabel("projectDetails_star", textString: "\(JSON(detailProject.starsCount!))")
        watchedCountLabel.attributedText = self.setImageForLabel("projectDetails_watch", textString: "\(JSON(detailProject.watchesCount!))")
        
        
        let timeStr = "\(JSON(detailProject.createdAt!))"
        if !(timeStr.isEmpty) {
            createdTimeLabel.attributedText = self.setImageForLabel("projectDetails_time", textString: timeStr.substringToIndex(timeStr.startIndex.advancedBy(10)))
        } else {
            createdTimeLabel.attributedText = self.setImageForLabel("projectDetails_time", textString: timeStr)
        }
        
        //\(JSON(detailProject.forksCount)"
        branchLabel.attributedText = self.setImageForLabel("projectDetails_fork", textString: "\(JSON(detailProject.forksCount!))")
        publicLabel.attributedText = self.setImageForLabel("projectDetails_public", textString: "public")
        languageLabel.attributedText = self.setImageForLabel("projectDetails_language", textString: "\(JSON(detailProject.language!))")
    }
    
    func setImageForLabel(imageName: String, textString: String) -> NSAttributedString {
        let mutableattributedString = NSMutableAttributedString()
        
        let textAttachment = NSTextAttachment()
        let image = UIImage(named: imageName)
        textAttachment.image = image
        textAttachment.bounds = CGRect.init(x: 0, y: -2, width: (image?.size.width)!, height: (image?.size.height)!)
        let attributedString = NSAttributedString(attachment: textAttachment)
        
        mutableattributedString.appendAttributedString(attributedString)
        mutableattributedString.appendAttributedString(NSAttributedString(string: " \(textString)"))
        
        return mutableattributedString
    }
    
    
}
